package com.example.clock_demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page; // 导入 Page
import com.example.clock_demo.common.Result;
import com.example.clock_demo.dto.PunchRecordRequestDTO;
import com.example.clock_demo.entitys.PunchRecord;
import com.example.clock_demo.service.PunchRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat; // 导入 DateTimeFormat
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/punches")
@Api(tags = "打卡记录管理")
public class PunchRecordController {

    @Autowired
    private PunchRecordService punchRecordService;

    /**
     * 记录员工的原始打卡信息，并判断其有效性。
     * POST /punches/record
     * @param requestDTO 打卡请求数据
     * @return 保存后的打卡记录，包含是否有效字段
     */
    @PostMapping("/record")
    @ApiOperation("记录员工打卡")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<PunchRecord> recordPunch(@RequestBody PunchRecordRequestDTO requestDTO) {
        if (requestDTO.getEmployeeNumber() == null || requestDTO.getPunchTime() == null) {
            return Result.error("员工号和打卡时间不能为空");
        }
        try {
            PunchRecord savedRecord = punchRecordService.recordPunch(requestDTO);
            return Result.success(savedRecord);
        } catch (Exception e) {
            return Result.error("记录打卡信息失败: " + e.getMessage());
        }
    }

    /**
     * 查询任意人员在指定时间段内的原始打卡记录，支持模糊查询和分页。
     * GET /punches/search?startDate=2025-07-01&endDate=2025-07-31&teamName=生产&employeeName=张&pageNum=1&pageSize=10
     * @param startDate 查询开始日期 (格式: yyyy-MM-dd)
     * @param endDate 查询结束日期 (格式: yyyy-MM-dd)
     * @param teamName 班组名称 (模糊查询，可选)
     * @param employeeName 姓名 (模糊查询，可选)
     * @param pageNum 当前页码
     * @param pageSize 每页记录数
     * @return 分页的原始打卡记录列表
     */
    @GetMapping("/search")
    @ApiOperation("查询打卡记录")
    @PreAuthorize("hasAnyRole('ADMIN', 'EMPLOYEE')")
    public Result<Page<PunchRecord>> searchPunchRecords(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
            @RequestParam(required = false) String teamName,
            @RequestParam(required = false) String employeeName,
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize
    ) {
        try {
            Page<PunchRecord> result = punchRecordService.searchPunchRecords(startDate, endDate, teamName, employeeName, pageNum, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询打卡记录失败: " + e.getMessage());
        }
    }
}